﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef WIFIBURNDLG_H
#define WIFIBURNDLG_H

#include <vector>
#include <QWidgetAction>
#include "BurnDlg.h"
#include "ui_WifiBurnDlg.h"
#include "MultipleBurnDlg.h"

namespace BurnDlg {

class WifiBurnDlg : public BurnDlg {
public:
    explicit WifiBurnDlg(ChipType chipType = ChipType::WIFI5GNB, QWidget *pParent = nullptr); // 标准构造函数
    ~WifiBurnDlg() override;
    void WifiBurnDlgDelete();
    enum class BOOT_TYPE {
        BOOT_ROM,
        BOOT_LOADER
    };

    void InitBurnCtrl() override;
    void ResetUIAndFSM() override;
    bool ConfigComPort() override;
    void GetComSettingFromGloble(int &baud, int &databits,
        int &stopbits, QSerialPort::Parity &parity, int &flowctrl) const;
    void SetComSettingToGloble(int baud, int databits,
        int stopbits, QSerialPort::Parity parity, int flowctrl) const;
    void UpdateCmd();
    void GetCmdInfoArray(QMap<int, BIN_INFO> &infoArray) const;
    void UpdateList() override;
    bool UpdateList(BIN_INFO_ARRAY &info) const;
    void AnalyseBinFile(QString filePath);
    void LoadDlgConfig() const;
    void JudgeIsSelectAll() const;
    void FillParamAndShow(const QMap<int, BIN_INFO> &info);
    void GetBinInforArray(QMap<int, BIN_INFO> &infoArray, int row) const;
    void UpdateVersion(const QString &version) const;
    void GetAllPathFromGrid(std::vector<QString> &path) const;
    bool CheckAllPathInGrid(bool onlyNotify = false);
    bool CheckAllPath(std::vector<QString> &path, bool onlyNotify = false);
    void InitializeInterfaceControls();
    void InitializeInterfaceControlsAdd();
    void SaveSettingCfg() const;
    void InitGetSettingCfg() const;
    void InitMenus();
    bool IsSelectionRows() const;
    void IsEarseModeHide() const;
    void contextMenuEvent(QContextMenuEvent *event) override;

protected slots:
    void SlotShowMenuSettings() override;
    void SlotSetBurninterval2ms();
    void SlotSetBurninterval10ms();
    void SlotSaveConfig();
    virtual void SlotSettingSuccess() override;
    virtual void SlotBnClickedButtonSelectfile() override;
    void SlotImportConfig();
    void SlotButtonClickedCom();
    void SlotCbnSetfocusComboCom() const;
    virtual void SlotBnClickedButtonConn() override;
    void SlotCheckAutoBurnStateChanged(int state);
    void SlotCheckAutoDisconnectStateChanged(int state);
    void SlotBnClickedButtonImportEfuseConfig();
    void SlotBnClickedButtonReadEfuse();
    virtual void SlotBnClickedButtonStartburn() override;
    LRESULT SlotSwitchDfu(WPARAM wParam, LPARAM lParam);
    virtual void SlotAutoDisconnect(WPARAM wParam, LPARAM lParam) override;
    void SlotDelayDecodeNode();
    void SlotEarseModeCurrentIndexChanged(int index) override;
    void SlotNMKillfocusListBinPath(const NMHDR *pNMHDR, LRESULT *pResult);
    void SlotLvnItemchangedListBinPath(const QModelIndex &topLeft, const QModelIndex &bottomRight,
                                       const QVector<int> &roles = QVector<int>());
    void SlotInsertPlainText(const QString &editStr) override;
    void SlotmodelReset();
    void SlotClearPlainText() override;
    void SlotReplacePlainText(const QString &oldText, const QString &newText) override;
    void SlotMergeCurrentCharFormat(const QTextCharFormat &fmt) override;
    void SlotBnClickedSelectTarget();
    void SlotBnClickedReset();
    void SlotBnClickedEraseAll() const;
    void SlotBnClickedExport();
    void SlotBnClickedMultipleBurn();
    void SlotSetProgressValue(unsigned int progress) override;
    void SlotSetProgressRange(int min, int max) override;
    void SlotMultipleBurnDlgClose();
    void SlotBnClickedTableHeader(int logicalIndex) override;
    void SlotUpdateList();
    void SlotConfigTimerTimeOut();
    void SlotAutoBurnClick(bool checked);
    void SlotActionShow() const;
    void SlotAddAdrAction() const;
    void SlotSetBurninterval();
    void SlotCreateNewFwpkgFile();
    void SlotSaveSpiltFwpkgFile();
protected:
    void changeEvent(QEvent* event) override;
private:
    void SaveDlgConfig();
    void SavedComConfig2File(const QString &path) const;
    void InitSettingsConnect();
    void InitButtonConnect();
    void InitBurnCtrlConnect();
    void LoadComConfigFromFilePartOne(const QSettings &cfg) const;
    void LoadComConfigFromFile(const QString &path) const;
    void LoadFileConfigFromFile(const QString &path) const;
    void SavedBinInfoConfig2File(const QString &path) const;
    void SavedWinInfoConfig2File(const QString &path) const;
    void ActionShowActEnable(bool enable) const;

private:
    Ui::WifiBurnDlg *ui;
    QAction *actImportConfig;
    QAction *actSaveConfig;
    QAction *actSettings;
    QAction *actChangeChip;
    QAction *actAbout;
    QAction *addAdrAction;
    QComboBox *comboBoxBurnInterval;
    bool alwaysClear;
    bool isMultipleBurnOpen;
    bool isAutoDisconnect;
    QMap<int, U32> efuseStartBit;
    QMap<int, U32> efuseBitWidth;
    QMap<int, QString> efuseName;
    int eraseMode;
    bool isInserting;
    MultipleBurnDlg *multipleBurnDlg;
    QTimer *configTimer;
    QMenu *tableViewMenu;
};

}
#endif
